import apis from '../../http/apis'
import router from '../../router'
export default {
    namespaced: true,
    state: {
        currentUser: {},
    },
    mutations: {
        setCurrentUser(state, data) {
            state.currentUser = data;
        },
    },
    actions: {
        //销售员登录
        async salesMenLogin(context, salesMan) {
            const { data } = await apis.login.salesMenLogin(salesMan)
            if (data.status) {
                localStorage.token = data.token;
                router.push({ path: '/salesMenSystem' })
                localStorage.type = 0;
            } else {
                alert('用户名或密码错误')
            }
        },
        //获取登录信息
        async getUserInfo(context) {
            const { data } = await apis.login.getUserInfo({ token: localStorage.token })
            console.log(data);
            context.commit('setCurrentUser', data.data)
        },
        //退出登录
        quit(context) {
            localStorage.removeItem('token')
            context.dispatch('getUserInfo')
        },

        // 管理员登录
        async adminsLogin(context, admins) {
            const { data } = await apis.login.adminsLogin(admins);
            console.log(data);
            if (data.status) {
                alert("登录成功");
                localStorage.token = data.token;
                localStorage.type = 1;
                router.push({ path: '/adminsSystem' })
            } else {
                alert("账号或密码错误，或者该账号已禁用");
            }
        },
    }
}